package address.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import address.model.AddressService;

import to.AddressTO2;
import to.ListTO3;
@Controller
public class AddressController {
	@Autowired
	private AddressService service;
	public AddressController(AddressService service){
		this.service = service;
	}
//	public ModelAndView modifyAddress(HttpServletRequest request, HttpServletResponse response, AddressTO ato){
//		ModelAndView mv = null;
//		
//		try {
//			//정보 수정 비지니스 로직에 요청
//			service.modifyAddress(ato);
//			
//			mv = new ModelAndView("");
//		} catch (Exception e) {
//			e.printStackTrace();
//			mv = new ModelAndView("/error.jsp", "error_message", e.getMessage());
//		}
//		return mv;
//	}
	//탑승주소 등록
//	@RequestMapping("/insertAddress.do")
//	public String insertAddress (HttpSession session, String memberPhone, SimpleDateFormat rideDate){
//		AddressTO ato = (AddressTO) session.getAttribute("newAddress");
//		ato.setMemberPhone(memberPhone);
//		ato.setRideDate(rideDate);
//		try{
//			service.insertAddress(ato);
//			session.setAttribute("resultMessage","--");
//		}catch(Exception e){
//			e.printStackTrace();
//			session.setAttribute("resultMessage", "에러");
//		}
//		return "firstPage.tiles";
//	}
//	//주소 조회
//	public ModelAndView selectAllAddress(HttpServletRequest request) throws SQLException{
//		ModelAndView mv = null;
//		HashMap<String,Object> condition = new HashMap<String,Object>();
//		@SuppressWarnings("unchecked")
//		Enumeration<String> paramNames = request.getParameterNames();
//		while(paramNames.hasMoreElements()){
//			String paramName = paramNames.nextElement();
//			condition.put(paramName, request.getParameter(paramName));
//		}
//		try{
//			ArrayList<AddressTO> list = service.selectAllAddress(condition);
//			
//			mv = new ModelAndView("address/Address_list.tiles","list",list);
//			
//		}catch(Exception e){
//			e.printStackTrace();
//			mv = new ModelAndView("/error.jsp", "error_message", e.getMessage());
//		}
//		return mv;
//	}
	@RequestMapping("/writeContent3.do")
	public ModelAndView writeContent(AddressTO2 ato){
		ModelAndView mv = null;
			
		//2. Business Logic - Model(Business Service)로 요청
		try {
			service.writeContent(ato);
			mv = new ModelAndView("redirect:getContent3.do?no="+ato.getNo());
		} catch (SQLException e) {
			e.printStackTrace();
			mv = new ModelAndView("address/error.jsp", "error_message", "글등록 중 오류 발생 : "+e.getMessage());
		}
		return mv;
	}
	@RequestMapping("/list3.do")
	public String list(@RequestParam(required=false, defaultValue="1")String page, Map map) throws Exception{
		System.out.println(page);
		
		String url = null;
		//2. Business Logic 호출 ->model		
		try {
			if(page.equals("")){
				page = "1";
			}
			int pageNo = Integer.parseInt(page);
			ListTO3 lto = service.getBoardListByPage(pageNo);
			url = "list.jsp";
			map.put("lto", lto);
		} catch (SQLException e) {
			e.printStackTrace();
			url = "error.jsp";
			map.put("error_message", "글목록 조회도중 오류 발생 : "+e.getMessage());
		}
		return url;
	}
	/*
	 * 글조회
	 */
	@RequestMapping("/getContent3.do")
	public ModelAndView getContent(int no){
		ModelAndView mv = null;
		try{
			//2. Business Logic 요청 - Model
			AddressTO2 ato = service.getContentByNO(no);
			mv = new ModelAndView("address/show_content.jsp", "ato", ato);
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("error.jsp", "error_message", "글 조회도중 오류 발생 : "+e.getMessage());
		}	
		return mv;
	}
	/**
	 * 수정폼 조회 처리
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/modifyForm3.do")
	public ModelAndView modifyForm(int no){
		ModelAndView mv = null;
		try{
			//2. B.L 처리
			AddressTO2 ato = service.getContentByNoForForm(no);
			mv = new ModelAndView("address/modify_form.jsp","ato", ato);
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("error.jsp","error_message", "수정할 글 조회도중 오류발생 : "+e.getMessage());
		}
		return mv;
	}
	/**
	 * 글 수정 처리 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/modifyContent3.do")
	public ModelAndView modifyContent(AddressTO2 ato) {
		ModelAndView mv = null;
		try {			
			//2. B.L 요청
			service.modifyContent(ato);
			mv = new ModelAndView("address/show_content.jsp", "ato", ato);
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("/error.jsp","error_message", "수정처리중 오류 발생 : "+e.getMessage());
		}
		return mv;
	}
	//삭제
	@RequestMapping("/deleteContent3.do")
	public ModelAndView deleteContent(int no){
		ModelAndView mv = null;
		try {
			//2. BL
			service.deleteContentByNO(no);
			mv = new ModelAndView("/list3.do");
			//mv = list(request, response);
		}catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.jsp","error_message", "삭제도중 오류 발생 : "+e.getMessage());
		}
		return mv;
	}
	/**
	 * 답변 폼 조회 처리
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/replyForm3.do")
	public ModelAndView replyForm(int no){
		ModelAndView mv = null;
		try {
			//1. 요청 파라미터 조회 - no
			AddressTO2 ato = service.getContentByNoForForm(no);
			mv = new ModelAndView("address/reply_form.jsp", "ato", ato);
		}catch (SQLException e) {
			e.printStackTrace();
			mv = new ModelAndView("error.jsp.jsp", "error_message", "답변할 원본글 조회도중 오류 발생 : "+e.getMessage());
		}
		return mv;
	}
	/**
	 * 답변 처리
	 * @param request
	 * @param response
	 * @param ato
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/replyContent3.do")
	public ModelAndView replyContent(AddressTO2 ato, int page) throws Exception{
		ModelAndView mv = null;
		try{
			service.replyContent(ato);
			//멱등처리
			mv = new ModelAndView("redirect:getContent3.do?no="+ato.getNo()+"&page="+page);
		}catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("/error.jsp", "error_message", "답변 처리 중 오류 발생 : "+e.getMessage());
		}
		return mv;
	}

}
